package com.ruoyi.activity.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.activity.domain.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;


/**
 * 商品Mapper
 */
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
    /**
     * 使用乐观锁更新库存
     * @param productId 商品ID
     * @param oldStock 原库存值（用于乐观锁校验）
     * @param newStock 新库存值
     * @param version 当前版本号
     * @return 更新影响的行数
     */
    @Update("UPDATE product SET stock = #{newStock}, version = version + 1 " +
            "WHERE id = #{productId} AND stock = #{oldStock} AND version = #{version}")
    int updateStockWithVersion(@Param("productId") Long productId,
                               @Param("oldStock") Integer oldStock,
                               @Param("newStock") Integer newStock,
                               @Param("version") Integer version);
} 